<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" style="text-align: left">
      <el-form-item label="连接状态">
        <el-select
          v-model="formInline.connectionStatus"
          placeholder="请选择连接状态"
        >
          <el-option label="请选择" value=""></el-option>
          <el-option label="在线" value="true"></el-option>
          <el-option label="离线" value="false"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备编号">
        <el-input
          v-model="formInline.equipMentNum"
          placeholder="请输入设备编号"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="load">查询</el-button>&nbsp;

        <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
      </el-form-item>
    </el-form>
    <br />
    <template>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          prop="equipMentNum"
          label="设备编号"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="principal"
          label="负责人"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column label="连接状态" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.connectionStatus == true">在线</span>
            <span v-else>离线</span>
          </template>
        </el-table-column>
        <el-table-column label="开启状态" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.openStatus == true">开启</span>
            <span v-else>关闭</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="openingTime"
          label="开启时间"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="closingTime"
          label="关闭时间"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
          width="200"
          align="center"
        ></el-table-column>
        <el-table-column prop="remark" label="操作" align="center">
          <template slot-scope="scope">
            <el-link target="_blank" @click="shezhi(scope.row)">设置</el-link>
            <el-link type="warning" @click="qidong(scope.row)">启动</el-link>
            <el-link type="warning" @click="guanbi(scope.row)">关闭</el-link>
            <el-link type="warning" @click="fantian(scope.row)">修改</el-link>
            <el-link type="danger" @click="shanchu(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="count"
    >
    </el-pagination>

    <el-dialog title="设备配置" :visible.sync="dialogFormVisible2">
      <el-form :model="from">
        <el-form-item label="设备编号" :label-width="formLabelWidth">
          <el-input v-model="form.equipMentNum" disabled></el-input>
        </el-form-item>

        <el-form-item label="开启时间" :label-width="formLabelWidth">
          <el-input v-model="form.openingTime" type="date"></el-input>
        </el-form-item>

        <el-form-item label="关闭时间" :label-width="formLabelWidth">
          <el-input v-model="form.closingTime" type="date"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="peizhishezhi">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增三色灯" :visible.sync="dialogFormVisible">
      <el-form :model="from">
        <el-form-item label="设备编号" :label-width="formLabelWidth">
          <el-input v-model="from.equipMentNum" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="连接状态" :label-width="formLabelWidth">
          <el-select
            v-model="from.connectionStatus"
            placeholder="请选择连接状态"
          >
            <el-option label="在线" :value="true"></el-option>
            <el-option label="离线" :value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否开启" :label-width="formLabelWidth">
          <el-select v-model="from.openStatus" placeholder="请选择连接状态">
            <el-option label="开启" :value="true"></el-option>
            <el-option label="关闭" :value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="负责人" :label-width="formLabelWidth">
          <el-input v-model="from.principal" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="list.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="tianjia">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改三色灯" :visible.sync="dialogFormVisible1">
      <el-form :model="list">
        <el-form-item label="设备编号" :label-width="formLabelWidth">
          <el-input v-model="list.equipMentNum" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="连接状态" :label-width="formLabelWidth">
          <el-select
            v-model="list.connectionStatus"
            placeholder="请选择连接状态"
          >
            <el-option label="在线" :value="true"></el-option>
            <el-option label="离线" :value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否开启" :label-width="formLabelWidth">
          <el-select v-model="list.openStatus" placeholder="请选择连接状态">
            <el-option label="开启" :value="true"></el-option>
            <el-option label="关闭" :value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="负责人" :label-width="formLabelWidth">
          <el-input v-model="list.principal" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="list.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="baocun">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { geTricolourLightapi } from "@/api/iotInfo/TricolourLight";
import { GetTotaocount } from "@/api/iotInfo/TricolourLight";
import { deleteTricolourLightapi } from "@/api/iotInfo/TricolourLight";
import { addTricolourLightapi } from "@/api/iotInfo/TricolourLight";
import { GetSelectTricolour } from "@/api/iotInfo/TricolourLight";
import { updateTricolourLightapi } from "@/api/iotInfo/TricolourLight";
import { UpdateSelectTricol1 } from "@/api/iotInfo/TricolourLight";

export default {
  data() {
    return {
      form: {
        id: 0,
        equipMentNum: "",
        principal: "",
        connectionStatus: true,
        openStatus: true,
        openingTime: "",
        closingTime: "",
        remark: "",
      },
      dialogFormVisible2: false,
      dialogFormVisible1: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      formInline: {
        pageIndex: 1,
        pageSize: 2,
        connectionStatus: "",
        equipMentNum: "",
      },
      countnum: 0,
      count: 0,
      tableData: [],
      multipleSelection: [],
      from: {
        id: 0,
        equipMentNum: "",
        principal: "",
        connectionStatus: true,
        openStatus: true,
        openingTime: "",
        closingTime: "",
        remark: "",
      },
      ids: {
        id: 0,
        equipMentNum: "",
        principal: "",
        connectionStatus: true,
        openStatus: true,
        openingTime: "",
        closingTime: "",
        remark: "",
      },
      list: {
        id: 0,
        equipMentNum: "",
        principal: "",
        connectionStatus: true,
        openStatus: true,
        openingTime: "",
        closingTime: "",
        remark: "",
      },
    };
  },
  created() {
    this.load();
    this.totalcount();
  },
  methods: {
    load() {
      geTricolourLightapi(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      this.formInline.pageIndex = val;
      this.load();
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    shanchu(id) {
      if (!confirm("确认要删除吗")) {
        return;
      }

      this.ids.id = id;

      deleteTricolourLightapi(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.load();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    tianjia() {
      addTricolourLightapi(this.from).then((res) => {
        if (res.data) {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.load();
          this.dialogFormVisible = false;
        } else {
          this.$message.error("添加失败");
        }
      });
    },
    fantian(row) {
      this.dialogFormVisible1 = true;

      this.list = row;
    },
    baocun() {
      updateTricolourLightapi(this.list).then((res) => {
        if (res.data) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.load();
          this.dialogFormVisible = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    guanbi(row) {
      let fantianS = {
        id: 0,
        equipMentNum: "",
        principal: "",
        connectionStatus: true,
        openStatus: true,
        openingTime: "",
        closingTime: "",
        remark: "",
      };

      fantianS = row;

      GetSelectTricolour(fantianS).then((res) => {
        if (res.data) {
          this.load();
          this.dialogFormVisible = false;
        }
      });
    },
    qidong(row) {
      let fantianS = {
        id: 0,
        equipMentNum: "",
        principal: "",
        connectionStatus: true,
        openStatus: true,
        openingTime: "",
        closingTime: "",
        remark: "",
      };

      fantianS = row;

      UpdateSelectTricol1(fantianS).then((res) => {
        if (res.data) {
          this.load();
          this.dialogFormVisible1 = false;
        }
      });
    },
    shezhi(row) {
      this.dialogFormVisible2 = true;

      this.form = row;
    },
    peizhishezhi() {
      updateTricolourLightapi(this.form).then((res) => {
        if (res.data) {
          this.$message({
            message: "设置成功",
            type: "success",
          });
          this.load();
          this.dialogFormVisible2 = false;
        } else {
          this.$message.error("设置失败");
        }
      });
    },
  },
};
</script>